package cc.zy.base.hr.service.impl;

import cc.zy.base.businesses.entity.College;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.ScoreMapping;
import cc.zy.base.hr.mapper.ScoreMappingMapper;
import cc.zy.base.hr.service.IScoreMappingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 2 * @Author: Junjie
 * 3 * @Date: 2022/3/9 10:34
 * 4
 */

@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ScoreMappingServiceImpl extends ServiceImpl<ScoreMappingMapper, ScoreMapping> implements IScoreMappingService {

    @Resource
    private final ScoreMappingMapper scoreMappingMapper;



    @Override
    public IPage<ScoreMapping> findScoreMapping(QueryRequest request, ScoreMapping scoreMapping) {

        Page<ScoreMapping> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countScoreMappingDetail(scoreMapping));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.findScoreMappingDetailPage(page,scoreMapping);
    }


    @Override
    public ScoreMapping findById(Integer id) {

        return baseMapper.findById(id);
    }


    @Override
    public List<ScoreMapping> findScoreMappings(ScoreMapping scoreMapping) {


        LambdaQueryWrapper<ScoreMapping> queryWrapper = new LambdaQueryWrapper<>();

        return baseMapper.selectList(queryWrapper);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createScoreMapping(ScoreMapping scoreMapping) {

        scoreMapping.setCreateDate(new Date());
        scoreMapping.setLastModifyDate(new Date());
        this.save(scoreMapping);

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateScoreMapping(ScoreMapping scoreMapping) {

        this.saveOrUpdate(scoreMapping);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteScoreMapping(ScoreMapping scoreMapping) {

        LambdaQueryWrapper<ScoreMapping> wrapper = new LambdaQueryWrapper<>();

        this.remove(wrapper);
    }



    @Override
    public void deleteScoreMappings(String[] scoreMappingIds) {

        List<String> list = Arrays.asList(scoreMappingIds);
        // 删除积分规则
        this.removeByIds(list);

    }

    @Override
    public List<ScoreMapping> findScoreMappingListNotPage(ScoreMapping scoreMapping) {

        List<ScoreMapping> scoreMappingListNotPage = baseMapper.findScoreMappingListNotPage(scoreMapping);
        return scoreMappingListNotPage;
    }

    @Override
    public Boolean isRepetitive(BigDecimal maxValue,BigDecimal minValue){
        List<ScoreMapping> repetitive = scoreMappingMapper.isRepetitive(maxValue,minValue);
        Boolean flag = true;
        if (repetitive != null && repetitive.size() > 1) {
            flag = false;
        }
        return flag;
    }

}
